using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.SessionState;
using AppEnvironment.Lib;
using AppEnvironment.Lib.Interfaces;

namespace AppEnvironment.Web
{
    internal class WebSession : Singleton<WebSession>, IStateHolder, IClearable, IUniquelyNamed
    {
        private WebSession()
        {

        }

        #region IStateHolder Members

        public object this[string key]
        {
            get
            {
                if (HttpContext.Current.Session == null)
                {
                    return null;
                }
                return HttpContext.Current.Session[key];
            }
            set
            {
                if (HttpContext.Current.Session == null)
                {
                    throw new Exception("Session object is null at this time");
                }
                HttpContext.Current.Session[key] = value;
            }
        }

        public void Clear()
        {
            if (HttpContext.Current.Session == null)
            {
                throw new Exception("Session object is null at this time");
            }
            HttpContext.Current.Session.Abandon();
        }

        #endregion

        #region IUniquelyNamed Members

        public string Id
        {
            get
            {
                if (HttpContext.Current.Session == null)
                {
                    throw new Exception("Session object is null at this time");
                }
                return HttpContext.Current.Session.SessionID;
            }
        }

        #endregion
    }
}
